package com.awise.view;

/**
 * Created by lgz on 2018/2/26.
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.awise.util.Constant;
import com.awise.util.MiscUtil;

import static android.content.ContentValues.TAG;

/**
 * 自定义绚丽的ProgressBar.
 */
public class ProgressBarView extends View {
    /**
     * 进度条所占用的角度
     */
    private static final int ARC_FULL_DEGREE = 360;
    /**
     * 弧线的宽度
     */
    private int STROKE_WIDTH;


    /**
     * 组件的宽，高
     */
    private int width, height;
    /**
     * 进度条最大值和当前进度值
     */
    private float max, progress;


    /**
     * 是否允许拖动进度条
     */
    private boolean draggingEnabled = false;


    /**
     * 绘制弧线的矩形区域
     */
    private RectF circleRectF;


    /**
     * 绘制弧线的画笔
     */
    private Paint progressPaint;
    /**
     * 绘制文字的画笔
     */
    private Paint textPaint;
    /**
     * 绘制当前进度值的画笔
     */
    private Paint thumbPaint;


    /**
     * 圆弧的半径
     */
    private int circleRadius;
    /**
     * 圆弧圆心位置
     */
    private int centerX, centerY;

    //默认大小
    private int mDefaultSize;


    private Rect textBounds = new Rect();
    private String text1="5",text2="6",text3="7";


    public ProgressBarView(Context context) {
        super(context);
        init(context);
    }


    public ProgressBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    public ProgressBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context mContext) {
        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);


        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);


        thumbPaint = new Paint();
        thumbPaint.setAntiAlias(true);

        mDefaultSize = MiscUtil.dipToPx(mContext, Constant.DEFAULT_SIZE);

        //使用自定义字体
//        textPaint.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fangz.ttf"));
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(MiscUtil.measure(widthMeasureSpec, mDefaultSize),
                MiscUtil.measure(heightMeasureSpec, mDefaultSize));


      /*  if (width == 0 || height == 0) {
            width = getWidth();
            height = getHeight();
            width=600;
            height=600;


            //计算圆弧半径和圆心点
            circleRadius = Math.min(width, height) / 2;
            STROKE_WIDTH = circleRadius / 12;
            circleRadius -= STROKE_WIDTH;


            centerX = width / 2;
            centerY = height / 2;


            //圆弧所在矩形区域
            circleRectF = new RectF();
            circleRectF.left = centerX - circleRadius;
            circleRectF.top = centerY - circleRadius;
            circleRectF.right = centerX + circleRadius;
            circleRectF.bottom = centerY + circleRadius;
        }*/
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        float start =270 + ((360 - ARC_FULL_DEGREE) >> 1); //进度条起始点
        float sweep1 = ARC_FULL_DEGREE * (progress / max); //进度划过的角度
        float sweep2 = ARC_FULL_DEGREE - sweep1; //剩余的角度


        //绘制起始位置小圆形
//        progressPaint.setColor(Color.WHITE);
        progressPaint.setColor(Color.parseColor("#62F3FC"));
        progressPaint.setStrokeWidth(0);
        progressPaint.setStyle(Paint.Style.FILL);
        float radians = (float) (((360.0f - ARC_FULL_DEGREE) / 2) / 180 * Math.PI);
        float startX = centerX - circleRadius * (float) Math.sin(radians);
        float startY = centerY + circleRadius * (float) Math.cos(radians);
        canvas.drawCircle(startX, startY, STROKE_WIDTH / 2, progressPaint);

        //绘制进度条
        progressPaint.setStrokeWidth(STROKE_WIDTH);
        progressPaint.setStyle(Paint.Style.STROKE);//设置空心
        canvas.drawArc(circleRectF, start, sweep1, false, progressPaint);
        //绘制进度条背景
        progressPaint.setColor(Color.parseColor("#172F46"));
        canvas.drawArc(circleRectF, start + sweep1, sweep2, false, progressPaint);


      /*  //绘制结束位置小圆形
        progressPaint.setStrokeWidth(0);
        progressPaint.setStyle(Paint.Style.FILL);
        float endX = centerX + circleRadius * (float) Math.sin(radians);
        float endY = centerY + circleRadius * (float) Math.cos(radians);
        canvas.drawCircle(endX, endY, STROKE_WIDTH / 2, progressPaint);*/

        //上一行文字
        textPaint.setTextSize(circleRadius >> 1);
        String text = (int) (100 * progress / max) + "";
        float textLen = textPaint.measureText(text);
        //计算文字高度
        textPaint.getTextBounds(text, 0, 1, textBounds);
        float h1 = textBounds.height();
//        % 前面的数字水平居中，适当调整
        float extra = text.startsWith("1") ? -textPaint.measureText("1") / 2 : 0;
//        canvas.drawText(text, centerX - textLen / 2 + extra, centerY - 30 + h1 / 2, textPaint);
        //百分号
        textPaint.setTextSize(circleRadius >> 2);
//        canvas.drawText("%", centerX + textLen / 2 + extra + 5, centerY - 30 + h1 / 2, textPaint);

        //下一行文字5
        textPaint.setTextSize(circleRadius / 5);
        textLen = textPaint.measureText(text1);
        textPaint.getTextBounds(text1, 0, text1.length(), textBounds);
        float h2 = textBounds.height();
        canvas.drawText(text1, centerX - textLen / 2, centerY -circleRadius+STROKE_WIDTH+ h2, textPaint);

        //下一行文字6
        textPaint.setTextSize(circleRadius / 5);
        textLen = textPaint.measureText(text2);
        textPaint.getTextBounds(text2, 0, text2.length(), textBounds);
        canvas.drawText(text2, centerX +circleRadius*3/4- textLen , centerY +circleRadius/2, textPaint);


        //下一行文字7
        textPaint.setTextSize(circleRadius / 5);
        textLen = textPaint.measureText(text3);
        textPaint.getTextBounds(text3, 0, text3.length(), textBounds);
        canvas.drawText(text3, centerX -circleRadius*3/4, centerY +circleRadius/2, textPaint);


        //绘制进度位置，也可以直接替换成一张图片
        float progressRadians = (float) (((360.0f /*- ARC_FULL_DEGREE*/) / 2 + sweep1) / 180 * Math.PI);
        float thumbX = centerX - circleRadius * (float) Math.sin(progressRadians);
        float thumbY = centerY + circleRadius * (float) Math.cos(progressRadians);
      /*  thumbPaint.setColor(Color.parseColor("#33d64444"));
        canvas.drawCircle(thumbX, thumbY, STROKE_WIDTH * 2.0f, thumbPaint);
        thumbPaint.setColor(Color.parseColor("#99d64444"));
        canvas.drawCircle(thumbX, thumbY, STROKE_WIDTH * 1.4f, thumbPaint);*/
//        thumbPaint.setColor(Color.WHITE);
        thumbPaint.setColor(Color.parseColor("#62F3FC"));
        canvas.drawCircle(thumbX, thumbY, STROKE_WIDTH * 0.8f, thumbPaint);
//        System.out.println("progressPaint thumbX="+thumbX+",thumbY="+thumbY);
    }
    public   int morning=0;
    public  int night=1;
    private int timeType;
    public void setTime(int timeType){
        if (timeType==morning){
            text1="5";
            text2="6";
            text3="7";
        }else if (timeType==night){
            text1="17";
            text2="18";
            text3="19";
        }
        this.timeType=timeType;
        invalidate();
    }
    private TimeListener listener;
    public interface TimeListener{
        public void setRefreshTimeListener(int progress,int timeType);
    }
    public void setOnRefreshTimeListener(TimeListener  listener){
        this.listener=listener;
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.d(TAG, "onSizeChanged: w = " + w + "; h = " + h + "; oldw = " + oldw + "; oldh = " + oldh);
            width = w;
            height = h;

            //计算圆弧半径和圆心点
//            circleRadius = Math.min(width, height) / 2;
            circleRadius = Math.min(width - getPaddingLeft() - getPaddingRight() ,
                    height - getPaddingTop() - getPaddingBottom()) / 2;
            STROKE_WIDTH = circleRadius /8;
            circleRadius -= STROKE_WIDTH;


            centerX = width / 2;
            centerY = height / 2;


            //圆弧所在矩形区域
            circleRectF = new RectF();
            circleRectF.left = centerX - circleRadius;
            circleRectF.top = centerY - circleRadius;
            circleRectF.right = centerX + circleRadius;
            circleRectF.bottom = centerY + circleRadius;

    }



    private boolean isDragging = false;


    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (!draggingEnabled) {
            return super.onTouchEvent(event);
        }


        //处理拖动事件
        float currentX = event.getX();
        float currentY = event.getY();


        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //判断是否在进度条thumb位置
                if (checkOnArc(currentX, currentY)) {
                    float newProgress = calDegreeByPosition(currentX, currentY) / ARC_FULL_DEGREE * max;
                    setProgressSync(newProgress);
                    isDragging = true;
                }
                break;


            case MotionEvent.ACTION_MOVE:
                if (isDragging) {
                    //判断拖动时是否移出去了
                    if (checkOnArc(currentX, currentY)) {
                        setProgressSync(calDegreeByPosition(currentX, currentY) / ARC_FULL_DEGREE * max);
                    } else {
                        isDragging = false;
                    }
                }
                break;


            case MotionEvent.ACTION_UP:
                isDragging = false;
                break;
        }


        return true;
    }


    private float calDistance(float x1, float y1, float x2, float y2) {
        return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }


    /**
     * 判断该点是否在弧线上（附近）
     */
    private boolean checkOnArc(float currentX, float currentY) {
        float distance = calDistance(currentX, currentY, centerX, centerY);
        float degree = calDegreeByPosition(currentX, currentY);
        return distance > circleRadius - STROKE_WIDTH * 5 && distance < circleRadius + STROKE_WIDTH * 5
                && (degree >= -8 && degree <= ARC_FULL_DEGREE + 8);
    }


    /**
     * 根据当前位置，计算出进度条已经转过的角度。
     */
    private float calDegreeByPosition(float currentX, float currentY) {
        float a1 = (float) (Math.atan(1.0f * (centerX - currentX) / (currentY - centerY)) / Math.PI * 180);
        if (currentY < centerY&currentX!= centerX) {
            a1 += 180;
        } else if (currentY > centerY && currentX > centerX) {
            a1 += 360;
        }


        return a1 - (360 - ARC_FULL_DEGREE) / 2;
    }


    public void setMax(int max) {
        this.max = max;
        invalidate();
    }


   /* public void setProgress(float progress) {
        final float validProgress = checkProgress(progress);
        //动画切换进度值
        new Thread(new Runnable() {
            @Override
            public void run() {
                float oldProgress = ProgressBarView.this.progress;
                for (int i = 1; i <= 100; i++) {
                    ProgressBarView.this.progress = oldProgress + (validProgress - oldProgress) * (1.0f * i / 100);
                    postInvalidate();
                    SystemClock.sleep(2);
                }
            }
        }).start();
    }*/

    public void setProgress(float progress) {
        this.progress  = checkProgress2(progress);
        invalidate();
    }




    public void setProgressSync(float progress) {
        this.progress = checkProgress(progress);
        if (listener!=null){
            listener.setRefreshTimeListener((int)this.progress,timeType);
        }
        invalidate();

    }


    //保证progress的值位于[0,max]
    private float checkProgress(float progress) {
        if (progress<=0) {
            return 0;
        }else if (progress<max&&progress>(max/2)){
            progress=  progress-(max/2) ;
        }else if (progress<(max/2)&&progress> 0){
            progress=progress+(max/2) ;

        }
        return progress > max ? max : progress;
    }


    //保证progress的值位于[0,max]
    private float checkProgress2(float progress) {
        if (progress <=0) {
            return 0;
        }
        return progress > max ? max : progress;
    }



    public void setDraggingEnabled(boolean draggingEnabled) {
        this.draggingEnabled = draggingEnabled;
    }
}